//=== EmualtorCodeGenerator.h - Class definition for the EmualtorJIT -*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the interface between the emulator interpreter and jit.
//
//===----------------------------------------------------------------------===//

#ifndef EMULATOR_CODE_GENERATOR_H
#define EMULATOR_CODE_GENERATOR_H 
#include <stdint.h>

namespace llvm {

class EmulatorCodeGenerator 
{
private:
  // ========================================
  // JIT snippet addresses.
  // ========================================
  void *enterJIT;
  void *exitJIT;
  void *linkStubJIT;
  void *dummyJIT;

  // The required code snippet generated.
  uint32_t initialized;

  /// private contor.
  EmulatorCodeGenerator() {}

  // ========================================
  // JIT snippet generation.
  // ========================================
  void *generateJITEntryTrampoline(uint8_t *StartAddr); 
  void *generateJITExitTrampoline(uint8_t *StartAddr); 
  void *generateJITLinkStubExit(uint8_t *StartAddr);
  void *generateJITDummy(uint8_t *StartAddr); 


public:
  // ========================================
  // JIT code generator initialization.
  // ========================================
  static EmulatorCodeGenerator *CreateDefaultCodeGenerator(void) 
  {
    EmulatorCodeGenerator *NewCG = new EmulatorCodeGenerator();
    NewCG->enterJIT = NewCG->exitJIT = 0;
    NewCG->initialized = 0;
    return NewCG;
  }
  /// return an estimation of the required amount required.
  uint32_t estimateRequiredCodeSize(void) 
  {
    uint32_t estimatedSize = 0;
    estimatedSize += 16;  // JIT entry trampoline.
    estimatedSize += 16;  // JIT exit trampoline.
    return estimatedSize;
  }
  /// initialize the code generator and generate 
  /// interpreter and jit related code snippets.
  void initialize(void *ExecMem, uint32_t Size);

  // ========================================
  // JIT snippet getters.
  // ========================================
  void *getJITEntryRoutine(void)    { return enterJIT;   }
  void *getJITExitRoutine(void)     { return exitJIT;    }
  void *getJITLinkStubRoutine(void) { return linkStubJIT;}
  void *getJITDummyRoutine(void)    { return dummyJIT;   }
};

} // End llvm namespace

#endif
